package kburndown.client.app.controls
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	
	import mx.core.IVisualElementContainer;
	
	import spark.core.SpriteVisualElement;
	import spark.effects.Fade;
	import spark.effects.Resize;

	public class TaskListShadow extends SpriteVisualElement
	{
		public function TaskListShadow()
		{
			super();

			this.mouseEnabled = false;
			this.mouseChildren = false;
			
			_fadeIn = new Fade();
			_fadeIn.alphaTo = 1;
			_fadeIn.startDelay = 900;

			_fadeOut = new Fade();
			_fadeOut.alphaTo = 0;
			_fadeOut.duration = 200;
			
			_resize = new Resize();
//			_resize.duration = 
		}

		private var _lastShadow: Sprite;
		private var _fadeIn: Fade;
		private var _fadeOut: Fade;
		private var _resize: Resize;

		public function drawShadow(x: Number, y: Number, width: Number, height: Number, cornerRadius: Number): void
		{
			var _owner: IVisualElementContainer = IVisualElementContainer(this.owner);
			_owner.setElementIndex(this, _owner.numElements - 1);
			
			if (_lastShadow)
			{
				if (_lastShadow.x == x && _lastShadow.y == y)
				{
					_fadeIn.play([_lastShadow]);
					_resize.widthTo = width;
					_resize.heightTo = height;
					_resize.play([_lastShadow]);
					
					return;
				}
				
				_fadeIn.stop();
				_fadeOut.play([_lastShadow]);
			}

			var shadowSprite: Sprite = new Sprite();
			shadowSprite.x = x;
			shadowSprite.y = y;
			shadowSprite.alpha = 0;
			shadowSprite.filters = [new GlowFilter(0, 1, 6, 6, 2, 1, false, true), new DropShadowFilter(1, 90, 0x171717, 1, 4, 4, 1, 1, true, true)];
			var g: Graphics = shadowSprite.graphics;
			g.clear();
			g.beginFill(0xFF9900);
			g.drawRoundRect(0, 0, width, height, 2 * cornerRadius);
			g.endFill();
			this.addChild(shadowSprite);
			
			_fadeIn.play([shadowSprite]);
			_lastShadow = shadowSprite;
		}

		public function clearShadow(): void
		{
			if (_lastShadow)
			{
				_fadeIn.stop();
				_fadeOut.play([_lastShadow]);
			}
		}
	}
}